package phantri.uit.menunhahang;

import java.security.SecureRandom;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.ksoap2.serialization.SoapObject;

/**
 * Contains all common functions for re-use
 * 
 * @author BINH
 * @version 1.0
 * */
public final class MenuCalculate {

	private static final String PATTERN = "\\b[a-z0-9._%+-]+@[a-z0-9._-]+\\.[a-z]{2,4}\\b";
	private static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	/**
	 * Check if the input email is valid.
	 * 
	 * @param email
	 *            type of {@link String} the user input email needs checking.
	 * @return true if the email is valid, false if the email is not valid.
	 * */
	public static boolean checkValidEmail(String email) {
		Pattern p = Pattern.compile(PATTERN);
		Matcher matcher = p.matcher(email);

		return matcher.matches();
	}

	/**
	 * Return the random alphanumeric string
	 * 
	 * @param length
	 *            the length of the random string
	 * @return {@link String} return value
	 * */
	public static String randomAlphaNumeric(int length) {
		SecureRandom s = new SecureRandom();
		StringBuilder sb = new StringBuilder(length);
		for (int i = 0; i < length; i++) {
			sb.append(AB.charAt(s.nextInt(AB.length())));
		}

		return sb.toString();
	}

	public static int anyTypeStringToInt(Object obj) {
		String st = anyTypeToEmptyString(obj);
		if (st.equals(""))
			return 0;

		String r = "0";
		for (int i = 0; i < st.length(); i++) {
			char c = st.charAt(i);
			if (c - 48 >= 0 && c - 48 <= 9) {
				r = r + c;
			} else {
				break;
			}
		}
		return Integer.parseInt(r);
	}

	/**
	 * Check whether the input string is empty
	 * 
	 * @param s
	 * @return return true if sting is null, empty or space else return false
	 * */
	public static boolean isEmptyString(String s) {
		if (s.trim().length() == 0 || s == null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Convert an any type object to string. Note that any type object is a soap
	 * object
	 * 
	 * @param obj
	 * @return if obj is null return "", else return obj.toString()
	 * */
	public static String anyTypeToEmptyString(Object obj) {
		try {
			SoapObject temp = (SoapObject) obj;
			if (temp.getPropertyCount() == 0) {
				return String.valueOf("");
			}
		} catch (ClassCastException e) {
			// Just check type of obj
		}

		// obj is primitive type
		return obj.toString();
	}

	/**
	 * Convert an any type object(soap object) to integer.
	 * 
	 * @param obj
	 * @return if obj is null, return 0. If obj is not null, return its value.
	 */
	public static int anyTypeToInt(Object obj) {
		String st = anyTypeToEmptyString(obj);
		int result = 0;
		if (st.equals(""))
			return 0;
		try {
			result = Integer.parseInt(st);
			return result;
		} catch (ClassCastException e) {
			return 0;
		}
	}

	public static double StringToDouble(String st) {
		try {
			return Double.parseDouble(st);
		} catch (ClassCastException e) {
			return 0;
		}
	}

	@Deprecated
	public static Date StringToDate(String sDate) {
		Date date = new Date();
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		try {
			date = fmt.parse(sDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static String getMessage(Exception e) {
		StringBuilder sb = new StringBuilder();
		while (e != null) {
			sb.append(e.toString());
			for (int i = 0; i < e.getStackTrace().length; i++) {
				sb.append("\n\tat " + e.getStackTrace()[i].toString());
			}

			e = (Exception) e.getCause();
			if (e != null) {
				sb.append("\nCaused by: ");
			}
		}
		return sb.toString();
	}

	public static double GetDistance(double lat1, double long1, double lat2,
			double long2) {
		double result = 0;
		int R = 6371;
		double dLat = Math.toRadians(lat2 - lat1);
		double dLon = Math.toRadians(long2 - long1);
		double dLat1 = Math.toRadians(lat1);
		double dLat2 = Math.toRadians(lat2);
		double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(dLat1)
				* Math.cos(dLat1) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
		result = R * c;
		return result;
	}
}
